package com.zxn.xiaoran.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.zcommon.lib.SystemIntent;
import com.zcommon.lib.UIUtils;
import com.zxn.presenter.presenter.CreatePresenter;
import com.zxn.xiaoran.R;
import com.zxn.xiaoran.presenter.AppViewPresenter;
import com.zxn.xiaoran.utils.CommonUtil;
import com.zxn.xiaoran.view.CommonDgFrag;
import com.zxn.xiaoran.view.base.XMActivity;
import com.zxn.xm.text.FadeInTextView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by zxn on 2019-5-8 14:54:12.
 */
@CreatePresenter(AppViewPresenter.class)
public class SplashActivity extends XMActivity<AppViewPresenter> implements AppViewPresenter.IAppView {

    @BindView(R.id.tv_start)
    FadeInTextView tvStart;
    @BindView(R.id.iv_start)
    ImageView ivStart;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_splash;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
//        fvXm.setOnDismissListener(this);
//        mPresenter.appCheck();
//        Glide.with(this).load(R.mipmap.bg_start).into(ivStart);
        Animation animation = AnimationUtils.loadAnimation(mContext,
                R.anim.fade_in);
        tvStart.startAnimation(animation);
        tvStart.setVisibility(View.VISIBLE);
        int color = Color.argb(CommonUtil.random(76, 255), 0xff, CommonUtil.random(255), CommonUtil.random(255));
        tvStart.setTextColor(color);
        tvStart.setTextString(UIUtils.getString(R.string.text_splash_purpose))
                .startFadeInAnimation()
                .setTextAnimationListener(new FadeInTextView.TextAnimationListener() {
                    @Override
                    public void animationFinish() {
                        int color = Color.argb(CommonUtil.random(76, 255), 0xff, CommonUtil.random(255), CommonUtil.random(255));
                        tvStart.setTextColor(color);
                        toMain();
//                        if (mPresenter.hasNewVersion()) {
//                            /*showToast("发现新版本啦!");
//                            SystemIntent.jumpToBrowser(SplashActivity.this, mPresenter.getCheckInfo().data.downloadURL);
//                            finish();*/
//                            showDg();
//                        } else {
//                            toMain();
//                        }
                    }

                });


//        toMain();
    }

    private void showDg() {
        CommonDgFrag.newInstance(new CommonDgFrag.IDialogCreate() {
            @Override
            public void onConfirmClick(View view) {
                SystemIntent.jumpToBrowser(SplashActivity.this, mPresenter.getCheckInfo().data.downloadURL);
            }

            @Override
            public String getTitle() {
                return "温馨提示";
            }

            @Override
            public String getContent() {
                return "是否进行版本升级";
            }

            @Override
            public boolean isSingleButton() {
                return false;
            }

        }).show(getSupportFragmentManager());
    }

    private void toMain() {
        AndroidSchedulers.mainThread().scheduleDirect(new Runnable() {
            @Override
            public void run() {
                HomeActivity.jumpTo(SplashActivity.this);
//                SplashListActivity.jumpTo(SplashActivity.this);
                finish();
            }
        }, 1, TimeUnit.SECONDS);
    }


//    @SuppressLint("CheckResult")
//    private void intiRxPermissions() {
//        mPresenter.appCheck();
////        RxPermissions rxPermissions = new RxPermissions(this);
////        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
////                .subscribe(new Consumer<Boolean>() {
////                    @Override
////                    public void accept(Boolean aBoolean) throws Exception {
////                        if (!aBoolean) {
////                            UIUtils.toast("请开启读写sd卡权限");
////                        } else {
////                            mPresenter.appCheck();
////                            //UpdateUtils.clearDownload();
////                        }
////                    }
////                });
//
//    }


    @Override
    public void onAppCheck() {
        if (mPresenter.getCheckInfo() != null) {
//            UpdateUtils.APP_UPDATE_DOWN_APK_PATH = "apk" + File.separator + "downApk";
//            String desc = mPresenter.getCheckInfo().data.buildUpdateDescription + mPresenter.getCheckInfo().data.buildBuildVersion;
//            String apkName = SystemUtils.getVersionName(this);
            if (mPresenter.hasNewVersion()) {
                /*UpdateFragment.showFragment(this,
                        true, mPresenter.getCheckInfo().data.downloadURL, apkName, desc, getPackageName());*/
                showToast("发现新版本啦!");
                SystemIntent.jumpToBrowser(this, mPresenter.getCheckInfo().data.downloadURL);
                finish();
            } else {
                toMain();
            }
        } else {
            toMain();
        }
    }

//    @Override
//    public void onDismiss(FireworkDrawable fireworkDrawable) {
//        toMain();
//    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(R.id.toolbar)
                .navigationBarColor(R.color.c23)
                .init();
    }

}
